/*
 * Copyright 2008 Eckhart Arnold (eckhart_arnold@hotmail.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */


package tddd24.ajgallery.client.gwtphotoalbum;

import java.lang.String;
//import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.DomEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.user.client.ui.Image;

/**
 * Compatibility layer to evade a bug or feature (?) of the Internet Explorer
 * Browser.
 */
public class Compatibility {
  /** true, if the browser of the Internet Explorer family */
  static final boolean isIE;
  /** true, if the browser supports shadowed text */
  static final boolean supportsTextShadow;
  
  static {
    if (isInternetExplorer() == 1) isIE = true;
    else isIE = false;
    
    if (checkSupportsTextShadow() == 1)
      supportsTextShadow = true;
    else
      supportsTextShadow = false;
  }
  
  /**
   * Fires the <code>onLoad</code> events of the <code>LoadListeners</code>
   * manually in case the browser used is the MS Internet Explorer. This 
   * workaround is necessary, because the Internet Explorer sometimes omits 
   * to fire <code>onLoad</code>events.
   * 
   * <p><code>ieFireLoad</code> should be called <em>after</em> the 
   * <code>Image.setUrl()</code> method has 
   * been called if the image was not created
   * with a specific URL already.
   *  
   * @param img  the image for which the <code>LoadListeners</code> are to be
   *             notified of the image's being loaded.
   * @param url  the URL of the image. This is needed to keep trace of the 
   *             image that IE (supposedly) has cached. 
   * @param handler a {@link LoadHandler} to be notified or <code>null</code>
   */
  public static void fireLoadOnIE (Image img, String url, 
      LoadHandler handler) {
    if (isIE) {
      if (handler != null) {
        // handler.onLoad(img);
        NativeEvent loadEvent = Document.get().createLoadEvent();
        DomEvent.fireNativeEvent(loadEvent, img);
      }
    }
  }
    
  /**
   * Checks whether the current browser is an Internet Explorer Browser.
   * @return  true, if the browser used is the Internet Explorer
   */
  private static native int isInternetExplorer() /*-{
    var agent = navigator.userAgent.toLowerCase();
    if (agent.indexOf("msie") != -1) return 1;
    return 0;    
  }-*/; 

//    var agent = navigator.userAgent.toLowerCase();
//    var version=parseFloat(navigator.appVersion.split("MSIE")[1]);
//    if ((agent.indexOf("msie") != -1) && version < 8.0) return 1;
//    return 0;      

  
  /**
   * Checks whether the browser is new enough to support shadowed text.
   * @return  true, if the browser supports shadowed text
   */
  private static native int checkSupportsTextShadow() /*-{
    var agent = navigator.userAgent.toLowerCase();
    if (agent.indexOf("msie") != -1) return 1;
    return 0;    
  }-*/; 
   
}
